package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.api.vo.HealthDataListVo;
import com.yt.app.api.vo.HealthDataVo;
import com.yt.app.core.interfaces.HealthDataService;
import com.yt.app.core.interfaces.HealthDataTypeService;
import com.yt.app.core.model.HealthData;
import com.yt.app.core.model.HealthDataType;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "09.健康数据")
@RequestMapping("/healthData")
public class HealthDataController extends BaseAction{
    @Autowired
    private HealthDataService healthDataService;
    @Autowired
    private HealthDataTypeService healthDataTypeService;

    @ApiOperation(value="获取健康数据类型", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "健康数据类型", response = HealthDataType.class)})
    @RequestMapping("/typeList")
    public @ResponseBody  Object getTypeList(){
        MsgBean msgBean = new MsgBean();
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.setPagingBean(null);
        List<HealthDataType> typeList = healthDataTypeService.find(filter);
        msgBean.put("data", typeList);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="获取健康数据", httpMethod = "GET")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "健康数据", response = HealthDataListVo.class)
    })
    @RequestMapping("/dataList")
    public @ResponseBody  Object dataList(
            @ApiParam("上次最后一个ID,传递接口json中返回的lastId") @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        QueryFilter filter = new DynamicSQLFilterImpl();
        if(lastId > 0) {
            filter.addFilter("Q_id_L_GT", String.valueOf(lastId));
        }

        filter.addFilter("Q_uid_L_EQ", String.valueOf(uid));
        filter.setSortField("dateline desc , dataTypeId ", false);

        List<HealthData> healthDataList = healthDataService.find(filter);
        Map<Integer, HealthDataListVo> map = new HashMap<Integer, HealthDataListVo>();

        QueryFilter typeFilter = new DynamicSQLFilterImpl();
        filter.setPagingBean(null);
        List<HealthDataType> typeList = healthDataTypeService.find(typeFilter);
        Map<Integer, HealthDataType> typeMap = new HashMap<Integer, HealthDataType>();
        for(HealthDataType type : typeList){
            typeMap.put(type.getId().intValue(), type);
        }

        lastId = -1L;
        for(HealthData d : healthDataList){
            HealthDataListVo listVo = map.get(d.getDateline());
            if(listVo == null){
                listVo = new HealthDataListVo();
                listVo.setUid(uid);
                listVo.setDateline(d.getDateline());
                listVo.setDataList(new ArrayList<HealthDataVo>());
                map.put(d.getDateline(), listVo);
            }
            HealthDataVo dataVo = new HealthDataVo();
            dataVo.setDataTypeId(d.getDataTypeId());
            dataVo.setDataValue(d.getDataValue());
            dataVo.setDateline(d.getDateline());
            dataVo.setId(d.getId());
            HealthDataType t = typeMap.get(d.getDataTypeId());
            dataVo.setDataTypeName(t.getTypeName());
            dataVo.setDataTypeUnit(t.getUnit());
            listVo.getDataList().add(dataVo);
            lastId = d.getDateline().longValue();
        }

        msgBean.put("lastId", lastId);
        msgBean.put("data", map.values());

        return msgBean.returnMsg();
    }

    @ApiOperation(value="保存健康数据", httpMethod = "POST")
    @RequestMapping("/save")
    public @ResponseBody  Object save(
            @ApiParam(value = "数据，格式:日期(20171212),数据类型,数据值;....", required = true) @RequestParam("data") String data,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request
            ){
        MsgBean msgBean = new MsgBean();
        Long uid = BaseVo.getUIDFromHeader(request);
        String[] da = data.split(";");
        long currTime = System.currentTimeMillis()/1000;
        for(String d : da){
            String [] field = d.split(",");
            HealthData  m = new HealthData();
            m.setDateline(Integer.parseInt(field[0]));
            m.setDataTypeId(Integer.parseInt(field[1]));
            m.setDataValue(field[2]);
            m.setUid(uid);
            m.setModifyTime(currTime);
            healthDataService.insert(m);
        }

        return msgBean.returnMsg();
    }

    @ApiOperation(value="删除健康数据", httpMethod = "GET")
    @RequestMapping("/delete")
    public @ResponseBody  Object delete(
            @ApiParam("健康数据ID") @RequestParam(value = "id", required = false, defaultValue = "0") Long id,
            @ApiIgnore BaseVo vo,
            @ApiIgnore HttpServletRequest request){
        MsgBean msgBean = new MsgBean();
        HealthData data = healthDataService.get(id);
        if(data == null){
            return msgBean.failure("健康数据不存在!").returnMsg();
        }
        healthDataService.remove(id);

        return msgBean.returnMsg();
    }
}
